package com.ls.demo.tcp.service.impl;

import com.ls.demo.bean.*;
import com.ls.demo.http.client.client.HttpClient;
import com.ls.demo.http.client.config.HttpClientProperties;
import com.ls.demo.http.client.view.RoomInfoView;
import com.ls.demo.tcp.namespace.Namespaces;
import com.ls.demo.tcp.service.CommandService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.java.Log;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author liShan
 * @date 2019/9/3 23:48
 */
@Log
@Service("command_61")
public class CommandRoomStatusImpl implements CommandService {

    @Resource
    private HttpClient httpClient;

    @Resource
    private HttpClientProperties properties;

    private Namespaces namespaces = Namespaces.getInstance();

    @Override
    public Command execute(ChannelHandlerContext ctx, Command command) {
        RoomBoxInfo info = RoomBoxInfo.getRoomBoxInfo(ctx, command, namespaces);
        List<String> datas = command.getDataList();
        RoomAirConditionerInfo airInfo = new RoomAirConditionerInfo();
        RoomBaseInfo baseInfo = new RoomBaseInfo();
        RoomLampInfo lampInfo = new RoomLampInfo();
        this.assignment(datas, airInfo, baseInfo, lampInfo);
        RoomInfo roomInfo = RoomInfo.builder()
                .roomBoxInfos(Stream.of(info).collect(Collectors.toList()))
                .airInfos(Stream.of(airInfo).collect(Collectors.toList()))
                .baseInfos(Stream.of(baseInfo).collect(Collectors.toList()))
                .lampInfos(Stream.of(lampInfo).collect(Collectors.toList()))
                .build();
        RoomInfoView view = httpClient.postForObject(properties.getHost(), properties.getPort(),
                properties.getSendStatusUri(), roomInfo, RoomInfoView.class, null);
        if (null != view){
            log.info(String.format("StatusCode: %s message: %s", view.getStatusCode(), view.getMessage()));
        }
        return null;
    }

    /**
     * 给参数赋值
     * @param data 16进制字符
     * @param airInfo 空调参数
     * @param baseInfo 房间基本参数
     * @param lampInfo 灯光参数
     */
    private void assignment(List<String> data, RoomAirConditionerInfo airInfo,
                            RoomBaseInfo baseInfo, RoomLampInfo lampInfo){
        int min = 7;
        this.assignment0(data.get(0), airInfo, baseInfo);
        this.assignment1(data.get(1), airInfo, baseInfo, lampInfo);
        this.assignment2(data.get(2), airInfo, baseInfo);
        this.assignment3(data.get(3), baseInfo);
        this.assignment4(data.get(4), airInfo, baseInfo);
        this.assignment5(data.get(5), lampInfo);
        this.assignment6(data.get(6), lampInfo);
        if (data.size() > min){
            this.assignment7(data.get(7), lampInfo);
            this.assignment8(data.get(8), lampInfo);
            this.assignment9(data.get(9), airInfo, baseInfo, lampInfo);
            this.assignment10(data.get(10), airInfo, baseInfo, lampInfo);
        }

    }

    private void assignment0(String data, RoomAirConditionerInfo airInfo, RoomBaseInfo baseInfo){
        int val = Integer.parseInt(data, 16);
        int index = 1;
        baseInfo.setSos((byte)(val & index));
        val >>= index;
        baseInfo.setCard((byte)(val & index));
        val >>= index;
        baseInfo.setClose((byte)(val & index));
        val >>= index;
        baseInfo.setClean((byte)(val & index));
        val >>= index;
        baseInfo.setUnDisturb((byte)(val & index));
        val >>= index;
        baseInfo.setHaveOne((byte)(val & index));
        val >>= index;
        airInfo.setMode((short) (val & 3));
    }

    private void assignment1(String data, RoomAirConditionerInfo airInfo,
                             RoomBaseInfo baseInfo, RoomLampInfo lampInfo){
        int val = Integer.parseInt(data, 16);
        int index = 1;
        lampInfo.setDimming1((byte)(val & index));
        val >>= index;
        baseInfo.setCheckOut((byte)(val & index));
        val >>= index;
        airInfo.setFans((short)(val & 3));
        val >>= 2;
        lampInfo.setDimming2((byte)(val & index));
        val >>= index;
        lampInfo.setDimming3((byte)(val & index));
        val >>= index;
        baseInfo.setAuth((byte)(val & index));
        val >>= 1;
        lampInfo.setDimming4((byte) (val & index));
    }

    private void assignment2(String data, RoomAirConditionerInfo airInfo, RoomBaseInfo baseInfo){
        int val = Integer.parseInt(data, 16);
        airInfo.setTestTemp((short)(val & 63));
        val >>= 6;
        baseInfo.setCheckInType((short)(val & 3));
    }

    private void assignment3(String data, RoomBaseInfo baseInfo){
        int val = Integer.parseInt(data, 16);
        baseInfo.setAuthDay((short) val);
    }

    private void assignment4(String data, RoomAirConditionerInfo airInfo, RoomBaseInfo baseInfo){
        int val = Integer.parseInt(data, 16);
        airInfo.setPreTemp((short)(val & 63));
        val >>= 6;
        baseInfo.setLaundry((byte) (val & 1));
        val >>= 1;
        baseInfo.setLaterState((byte) (val & 1));
    }

    private void assignment5(String data, RoomLampInfo lampInfo){
        int val = Integer.parseInt(data, 16);
        int index = 1;
        lampInfo.setBathroom1((byte)(val & index));
        val >>= index;
        lampInfo.setBathroom2((byte)(val & index));
        val >>= index;
        lampInfo.setBathroom3((byte)(val & index));
        val >>= index;
        lampInfo.setBathroomFan((byte)(val & index));
        val >>= index;
        lampInfo.setExtend4((byte)(val & index));
        val >>= index;
        lampInfo.setExtend3((byte)(val & index));
        val >>= 1;
        lampInfo.setExtend2((byte) (val & index));
        val >>= 1;
        lampInfo.setExtend1((byte) (val & index));
    }

    private void assignment6(String data, RoomLampInfo lampInfo){
        int val = Integer.parseInt(data, 16);
        int index = 1;
        lampInfo.setNight((byte)(val & index));
        val >>= index;
        lampInfo.setCorridor((byte)(val & index));
        val >>= index;
        lampInfo.setWardrobe((byte)(val & index));
        val >>= index;
        lampInfo.setLeftReading((byte)(val & index));
        val >>= index;
        lampInfo.setRightReading((byte)(val & index));
        val >>= index;
        lampInfo.setBar((byte)(val & index));
        val >>= 1;
        lampInfo.setRoom((byte) (val & index));
        val >>= 1;
        lampInfo.setFloor((byte) (val & index));
    }

    private void assignment7(String data, RoomLampInfo lampInfo){
        int val = Integer.parseInt(data, 16);
        int index = 1;
        lampInfo.setDimming5((byte)(val & index));
        val >>= index;
        lampInfo.setDimming6((byte)(val & index));
        val >>= index;
        lampInfo.setDimming7((byte)(val & index));
        val >>= index;
        lampInfo.setDimming8((byte)(val & index));
        val >>= index;
        lampInfo.setDimming9((byte)(val & index));
        val >>= index;
        lampInfo.setDimming10((byte)(val & index));
        val >>= 1;
        lampInfo.setDimming11((byte) (val & index));
        val >>= 1;
        lampInfo.setDimming12((byte) (val & index));
    }

    private void assignment8(String data, RoomLampInfo lampInfo){
        int val = Integer.parseInt(data, 16);
        int index = 1;
        lampInfo.setGuard1((byte)(val & index));
        val >>= index;
        lampInfo.setGuard2((byte)(val & index));
        val >>= index;
        lampInfo.setGuard3((byte)(val & index));
        val >>= index;
        lampInfo.setToiletFan((byte)(val & index));
        val >>= index;
        lampInfo.setExtend8((byte)(val & index));
        val >>= index;
        lampInfo.setExtend7((byte)(val & index));
        val >>= 1;
        lampInfo.setExtend6((byte) (val & index));
        val >>= 1;
        lampInfo.setExtend5((byte) (val & index));
    }

    private void assignment9(String data, RoomAirConditionerInfo airInfo,
                             RoomBaseInfo baseInfo, RoomLampInfo lampInfo){
        int val = Integer.parseInt(data, 16);
        airInfo.setTouchSource((short) val);
        baseInfo.setTouchSource((short) val);
        lampInfo.setTouchSource((short) val);
    }

    private void assignment10(String data, RoomAirConditionerInfo airInfo,
                              RoomBaseInfo baseInfo, RoomLampInfo lampInfo){
        int val = Integer.parseInt(data, 16);
        airInfo.setTouchPanel((short) val);
        baseInfo.setTouchPanel((short) val);
        lampInfo.setTouchPanel((short) val);
    }

}
